<?php

namespace App\Http\Controllers\Api;

use App\Model\ShopAttrKeyModel;
use App\Model\ShopAttrTkvModel;
use App\Model\ShopAttrValModel;
use App\Model\ShopGoodType;
use App\Model\ShopOrderFsModel;
use App\Model\ShopOrderFstModel;
use App\Model\SkuModel;
use App\Model\AddShop;

use App\Model\ShopGoodModel;
use App\Model\StoreGoodModel;
use App\Model\ShopGoodPicModel;
use App\Model\ShopAttrTypeModel;
use App\Http\Controllers\Controller;

class GoodController extends Controller
{
    //商品详情
    public function detail(){
    	//商品详情
        $good = ShopGoodModel::where('id',request()->id)->select('id','name','gid','picpath','picname','content','sale_num','attrtype_id','sell_price','is_attr','title','num','oss_pic')->where('state',1)->first();
        if(!$good){
            return ['code'=>0,'msg'=>retStrInfo(76)];
        }

        //商品信息
		$goods['id'] = $good['id'];
        //商品名
		$goods['name'] = $good['name'];
		if($good['title']){
			$goods['name'] = $good['title'];
		}
		//销量
		$goods['sale_num'] = $good['sale_num'];
		//销售价
		$goods['sell_price'] = number_format($good['sell_price'],2,".","");
		//主图
		$goods['main_pic'] = "http://".$_SERVER['HTTP_HOST'].$good['picpath'].$good['picname'];
		if($good['oss_pic']){
			$goods['main_pic'] = $good['oss_pic'];
		}else{
			if(empty($good['picname'])){
				$goods['main_pic'] = "http://".$_SERVER['HTTP_HOST'].'/img/good.png';
			}
		}
		//专柜价
		$goods['del_coin'] = AddShop::where('id',$good['gid'])->value('del_coin');

		//总入库量
		$goods['store_num'] = 0;
		//现有库存
		$goods['ku_num'] = $good['num'];
		//是否有规格
		$goods['is_attr'] = $good['is_attr'];
		// $goods['ku_num'] = 0;
		$goods['content'] = str_replace('/upload','http://'.$_SERVER['HTTP_HOST'].'/upload',$good['content']);
//		$goods['content'] = str_replace('alt','width',$goods['content']);
//		$goods['content'] = str_replace('undefined','100%',$goods['content']);
//		$goods['content'] = str_replace("align=\"absmiddle\"","width=100%",$goods['content']);
		$goods['content'] = str_replace("table width=\"790\"","table width=\"100%\"",$goods['content']);
		$goods['content'] = str_replace("<img ","<img style='width:100%' ",$goods['content']);


		//云仓库信息
		$store_good = StoreGoodModel::where('gid',$good['id'])->first();

		if($store_good){
			//总入库量
			$goods['store_num'] = StoreGoodModel::where('gid',$good['id'])->sum('num');

			//现有库存
			$sale_num = StoreGoodModel::where('gid',$good['id'])->sum('sale_num');

//			$goods['ku_num'] = $goods['store_num'];
		}

		if($good['oss_pic']){
			$piclist[] = $good['oss_pic'];
		}else{
			//商品图,轮播图
			if(empty($good['picname'])){
				$piclist[] = "http://".$_SERVER['HTTP_HOST'].'/img/good.png';
			}else{
				$piclist[] = "http://".$_SERVER['HTTP_HOST'].$good['picpath'].$good['picname'];
			}
		}


		$pics = ShopGoodPicModel::where('gid',$good['id'])->select('picname','oss_pic')->limit(5)->get();
		if(count($pics) > 0){
			foreach($pics as $v){
				if($v['oss_pic']){
					$piclist[] = $v['oss_pic'];
				}else{
					$piclist[] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
				}


			}
		}

		//规格
		$attr = [];
		$attr['attr_name'] = '';
		$attr['attr'] = [];
		//规格(判断是否存在规格)
		if($good['is_attr'] == 1){
			//规格类型
			$attr['attr_name'] = trim(ShopAttrTypeModel::attrname($good['attrtype_id']),',');

			//规格组合
			$kids = ShopAttrTkvModel::where('tid',$good['attrtype_id'])->distinct(true)->select('kid')->get();
			foreach($kids as $k=>$v){
				$kname = ShopAttrKeyModel::where('id',$v['kid'])->value('kname');
				$kids[$k]['kname'] = $kname;

				$vids = ShopAttrTkvModel::where('tid',$good['attrtype_id'])->where('kid',$v['kid'])->distinct(true)->select('vid')->get();
				foreach($vids as $key=>$val){
					$vname = ShopAttrValModel::where('id',$val['vid'])->value('vname');
					$vids[$key]['vname'] = $vname;
					$vids[$key]['kid_vid'] = $v['kid']."-".$val['vid'];
					if(empty($vname)){
						unset($vids[$key]);
					}
				}
				$kids[$k]['vids'] = $vids;
			}
			$attr['attr'] = $kids;
			//查询第一个规格的库存
			$good_attr_one = SkuModel::where('gid',$good['id'])->sum('sum');
			$goods['ku_num'] = $good_attr_one;
		}

		//更多好货
		$perfect = [];
		$list= [];
		// $tid = ShopGoodType::where('gid',$good['id'])->value('tid');

		// $list = ShopGoodType::where('tid',$tid)->where('gid','<>',$good['id'])->limit(10)->get();

		// foreach($list as $k=>$v){

		// 	//商品信息
		// 	$good = ShopGoodModel::where('id',$v['gid'])->first();
		// 	if($good['state'] != 1){
		// 		continue;
		// 	}
		// 	$perfect[$k]['id'] = $v['gid'];
		// 	$perfect[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$good['picpath'].$good['picname'];
		// 	if($good['oss_pic']){
		// 		$perfect[$k]['picname'] = $good['oss_pic'];
		// 	}else{
		// 		if(empty($good['picname'])){
		// 			$perfect[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].'/img/good.png';
		// 		}
		// 	}

		// 	$perfect[$k]['name'] = $good['name'];
		// 	if($good['title']){
		// 		$perfect[$k]['name'] = $good['title'];
		// 	}
		// 	$perfect[$k]['sell_price'] = number_format($good['sell_price'],2,".","");
		// }
		if(count($list) <= 0){
			$list = ShopGoodModel::orderBy('sell_price','asc')->where('state',1)->limit(3)->get();
			foreach($list as $k=>$v){
				$perfect[$k]['id'] = $v['id'];
				$perfect[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
				if($v['oss_pic']){
					$perfect[$k]['picname'] = $v['oss_pic'];
				}else{
					if(empty($v['picname'])){
						$perfect[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].'/img/good.png';
					}
				}

				$perfect[$k]['name'] = $v['name'];
				if($v['title']){
					$perfect[$k]['name'] = $v['title'];
				}
				$perfect[$k]['sell_price'] = number_format($v['sell_price'],2,".","");
			}
		}


		//加载
		$data['attr'] = $attr;
		$data['goods'] = $goods;
		$data['perfect'] = array_values($perfect);
		$data['piclist'] = $piclist;

        return ['code'=>1,'msg'=>'返回数据','data'=>$data];
    }

    //商品规格
	public function attr(){
		if(!request()->attr_id){
			return ['code'=>0,'msg'=>retStrInfo(111)];
		}
		if(!request()->gid){
			return ['code'=>0,'msg'=>retStrInfo(110)];
		}
		$data = [];
		//商品详情
		$list = SkuModel::where('gid',request()->gid)->where('property_id',request()->attr_id)->first();

		if($list){
			$data['id'] = $list['id'];
			$data['gid'] = request()->gid;
			$shop = ShopGoodModel::where('id',$list['gid'])->first();
			$data['name'] = $shop['name'];
			if($shop['title']){
				$data['name'] = $shop['title'];
			}
			$data['sell_price'] = number_format($list['sell_price'],2,".","");
			$data['attr'] = $list['attr'];
			$data['num'] = $list['sum'];
			if($list['sum'] <= 0){
				$data['num'] = '0';
			}
			$data['picname'] = "http://".$_SERVER['HTTP_HOST'].$list['picname'];
			if($list['oss_pic']){
				$data['picname'] = $list['oss_pic'];
			}
			$data['store_num'] = StoreGoodModel::where('gid',$list['gid'])->where('sku_id',$list['id'])->sum('num');
			if($data['store_num'] <= 0){
				$data['store_num'] = '0';
			}
			$data['sell_num'] = ShopOrderFsModel::where('sku_id',$list['id'])->sum('num');
			if($data['sell_num'] <= 0){
				$data['sell_num'] = '0';
			}

		}else{
			$goods = ShopGoodModel::where('id',request()->gid)->first();
			$data['id'] = 0;
			$data['gid'] = request()->gid;
			$data['name'] = $goods['name'];
			if($goods['title']){
				$data['name'] = $goods['title'];
			}
			$data['sell_price'] = '0';
			$data['attr'] = '';
			$data['num'] = '0';
			$data['picname'] = "http://".$_SERVER['HTTP_HOST'].$goods['picpath'].$goods['picname'];
			if($goods['oss_pic']){
				$data['picname'] = $goods['oss_pic'];
			}
			$data['store_num'] = '0';
			$data['sell_num'] = '0';
		}
		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}

}